package com.thwj.project.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.thwj.common.annotation.Log;
import com.thwj.common.core.controller.BaseController;
import com.thwj.common.core.domain.AjaxResult;
import com.thwj.common.core.page.TableDataInfo;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.utils.poi.ExcelUtil;
import com.thwj.project.biz.controller.vo.UserAuthenticationVO;
import com.thwj.project.biz.convert.UserInfoAdminResolver;
import com.thwj.project.biz.domain.UserAuthentication;
import com.thwj.project.biz.mapper.UserAuthenticationMapper;
import com.thwj.project.biz.service.IUserAuthenticationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户认证记录Controller
 *
 * @author generate
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/biz/authentication")
public class UserAuthenticationController extends BaseController {
    @Autowired
    private IUserAuthenticationService userAuthenticationService;
    @Autowired
    UserInfoAdminResolver userInfoAdminResolver;

/**
 * 查询用户认证记录列表
 */
@PreAuthorize("@ss.hasPermi('biz:authentication:list')")
@GetMapping("/list")
    public TableDataInfo list(UserAuthentication userAuthentication) {
        startPage();
        List<UserAuthentication> list = userAuthenticationService.getBaseMapper().selectList(new QueryWrapper<>(userAuthentication));
        //
        List<UserAuthenticationVO> authenticationVOList = list.stream().map(item->{
            UserAuthenticationVO authenticationVO = new UserAuthenticationVO();
            BeanUtils.copyProperties(item,authenticationVO);
            return authenticationVO;
        }).collect(Collectors.toList());
        userInfoAdminResolver.resolve(authenticationVOList,UserAuthenticationVO::getUserId,(obj, nickname, phone)->{
            obj.setNickName(nickname);
            obj.setUserPhone(phone);
        });

    return getDataTable(authenticationVOList,((Page)list).getTotal());
}

    /**
     * 导出用户认证记录列表
     */
    @PreAuthorize("@ss.hasPermi('biz:authentication:export')")
    @Log(title = "用户认证记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserAuthentication userAuthentication) {
        List<UserAuthentication> list = userAuthenticationService.list(new QueryWrapper<>(userAuthentication));
        ExcelUtil<UserAuthentication> util = new ExcelUtil<UserAuthentication>(UserAuthentication. class);
        util.exportExcel(response, list, "用户认证记录数据");
    }

    /**
     * 获取用户认证记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:authentication:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(userAuthenticationService.getById(id));
    }

    /**
     * 新增用户认证记录
     */
    @PreAuthorize("@ss.hasPermi('biz:authentication:add')")
    @Log(title = "用户认证记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserAuthentication userAuthentication) {
        return toAjax(userAuthenticationService.save(userAuthentication));
    }

    /**
     * 修改用户认证记录
     */
    @PreAuthorize("@ss.hasPermi('biz:authentication:edit')")
    @Log(title = "用户认证记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserAuthentication userAuthentication) {
        return toAjax(userAuthenticationService.updateById(userAuthentication));
    }




    /**
     * 审核用户认证
     */
    @PreAuthorize("@ss.hasPermi('biz:authentication:edit')")
    @Log(title = "用户认证记录", businessType = BusinessType.UPDATE)
    @PostMapping("auditUserAuthentication")
    public AjaxResult auditUserAuthentication(@RequestBody UserAuthentication userAuthentication) {
        return toAjax(userAuthenticationService.auditUserAuthentication(userAuthentication));
    }

    /**
     * 删除用户认证记录
     */
    @PreAuthorize("@ss.hasPermi('biz:authentication:remove')")
    @Log(title = "用户认证记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userAuthenticationService.removeByIds(Arrays.asList(ids)));
    }
}
